package com.allenkerr.web;

import com.allenkerr.model.Customer;
import com.allenkerr.service.CustomerService;
import com.allenkerr.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping(value = "/customers", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * curl -XPOST localhost:8089/customers
     */
    @PostMapping("")
    public Result saveCustomers() {
        List<Customer> customers = new ArrayList<>();
        customers.add(new Customer("Alice", "Smith"));
        customers.add(new Customer("Bob", "Smith"));
        customers.add(new Customer("Allen", "Kerr"));
        customers.add(new Customer("Tom", "Smith"));
        customers.add(new Customer("Alice", "Duke"));
        customers.add(new Customer("Amber", "Ayala"));

        return customerService.saveCustomers(customers);
    }

    @GetMapping("")
    public Result listCustomers() {
        Iterator<Customer> allCustomers = customerService.findAllCustomers();
        return Result.success(allCustomers);
    }

    @GetMapping("/first/{firstName}")
    public Result listCustomersByFirstName(@PathVariable String firstName) {
        List<Customer> customers = customerService.findCustomersByFirstName(firstName);
        return Result.success(customers);
    }

}